package kd.cosmic.formplugin;

import com.kingdee.util.StringUtils;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.form.control.Button;
import kd.bos.form.control.Control;
import kd.bos.form.control.events.BeforeClickEvent;
import kd.bos.form.plugin.AbstractFormPlugin;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.sdk.plugin.Plugin;

import java.util.EventObject;
import java.util.Random;

/**
 * 动态表单
 */
public class schoolCardApplyPlugin extends AbstractFormPlugin implements Plugin {
    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        Button xhzhButtonap = this.getView().getControl("xhzh_buttonap");
        xhzhButtonap.addClickListener(this);
    }

    @Override
    public void afterCreateNewData(EventObject e) {
        super.afterCreateNewData(e);
        //基础信息
        String userId = String.valueOf(RequestContext.get().getCurrUserId());
        String userName = String.valueOf(RequestContext.get().getUserName());
        this.getModel().setValue("name",userName);
        this.getModel().setValue("number",userId);
    }

    @Override
    public void beforeClick(BeforeClickEvent evt) {
        super.beforeClick(evt);
        Control source = (Control) evt.getSource();
        if(StringUtils.equals("xhzh_buttonap",source.getKey())){
            //TODO 审核tk_xhzh_school_card_apply的学号，姓名，身份证号，若通过
            // 继续核验该卡号是否为丢失状态，若是，或者该用户未注册过，则解锁“提交”，
            // 反之提示框提示“审核不通过”
            String userId = String.valueOf(RequestContext.get().getCurrUserId());
            DynamicObject[] info = BusinessDataServiceHelper.load("xhzh_school_card",
                    "number,xhzh_cardid,enable"
                    ,new QFilter[] {new QFilter("number", QCP.equals,userId)});
            if(this.getModel().getValue("xhzh_idnumber").equals("")){
                this.getView().showMessage("您的身份证号码处为空，请填写");
                evt.setCancel(true);
            }
            if(info == null || info.length == 0){
                this.getView().showMessage("信息审核成功，请提交等待管理员审核");
                return;
            }
            if(info[0].get("enable").equals("丢失")){
                this.getView().showMessage("请在校园卡信息查看处选择删除卡片后，再次进行申请");
                evt.setCancel(true);
            }
            else {
                this.getView().showMessage("您仍存在着已激活的校园卡或者挂失的校园卡，审核失败失败");
                evt.setCancel(true);
            }

        }
    }

    @Override
    public void click(EventObject evt) {
        super.click(evt);
        Control source = (Control) evt.getSource();
        String userId = String.valueOf(RequestContext.get().getCurrUserId());
        this.getView().setEnable(true,"bar_submit");
        if(StringUtils.equals("xhzh_buttonap",source.getKey())){
            //若通过审核，则随机生成7位卡号，同时进行判断，若存在，则while重新生成

            int cardnumber = new Random().nextInt(9000000)+1000000;
            boolean exists = QueryServiceHelper.exists("xhzh_school_card",
                    new QFilter[]{new QFilter("xhzh_cardid", QCP.equals,
                            String.valueOf(cardnumber))});
            while(exists){
                cardnumber = new Random().nextInt(9000000)+1000000;
                exists = QueryServiceHelper.exists("xhzh_school_card",
                        new QFilter[]{new QFilter("xhzh_cardid", QCP.equals,
                                String.valueOf(cardnumber))});
            }

            this.getModel().setValue("xhzh_cardid",String.valueOf(cardnumber));
            //TODO 后期寻找锁定提交按钮的操作

        }
    }
}